package com.education.admin.modules.miniprogram.controller;

import com.education.admin.common.Result;
import com.education.admin.modules.miniprogram.service.MiniSurveyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/mini/survey")
@RequiredArgsConstructor
public class MiniSurveyController {

    private final MiniSurveyService surveyService;

    @GetMapping("/{studentId}")
    public Result<Object> get(@PathVariable Long studentId,
                              @RequestParam(required = false) Long teacherId){
        return surveyService.getByStudent(studentId, teacherId);
    }

    @PostMapping("/save")
    public Result<Object> save(@RequestBody Map<String,Object> payload){
        return surveyService.save(payload);
    }

    /**
     * 预约/问卷列表：供“课程预约”页使用
     * 返回字段：id, studentId, name, avatar, courseTitle, timeDesc(格式化create_time), status
     */
    @GetMapping("/list")
    public Result<Object> list(){
        return surveyService.listForBooking();
    }

    // 状态更新：同意/拒绝
    @PostMapping("/{id}/approve")
    public Result<Object> approve(@PathVariable Long id){
        return surveyService.updateStatus(id, 1, null);
    }

    @PostMapping("/{id}/reject")
    public Result<Object> reject(@PathVariable Long id, @RequestBody Map<String,Object> body){
        String reason = body==null? null : String.valueOf(body.getOrDefault("reason",""));
        return surveyService.updateStatus(id, 2, reason);
    }
}


